package com.jmd.ui.tab.a_mapview.panel.left_tool_panel.sub;

import com.jmd.ApplicationSetting;
import com.jmd.browser.BrowserEngine;
import com.jmd.common.StaticVar;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.io.Serial;

@Component
public class LayerSelectorPanel extends JPanel {

    @Serial
    private static final long serialVersionUID = 3161455562222434180L;

    @Autowired
    private BrowserEngine browserEngine;

    private static final String OSM_NAME = "OSM(wgs84)(可能需要代理)";
    private static final String TIAN_NAME = "天地图(wgs84)";
    private static final String GOOGLE_NAME = "谷歌地图(gcj02)(可能需要代理)";
    private static final String AMAP_NAME = "高德地图(gcj02)";
    private static final String TENCENT_NAME = "腾讯地图(gcj02)";
    private static final String BING_NAME = "必应地图(gcj02)";
    private static final String BING_WGS84_NAME = "必应地图(wgs84)";
    private static final String BAIDU_NAME = "百度地图(仅预览)";
    private static final String ADDED_NAME = "自定义图层";

    @PostConstruct
    private void init() {

        this.setBorder(new TitledBorder(null, "图层选择", TitledBorder.LEADING, TitledBorder.TOP,
                StaticVar.FONT_SourceHanSansCNNormal_12, null));
        this.setLayout(new BorderLayout(0, 0));

        var layerTypeScrollPane = new JScrollPane();

        var layerTypeTree = new JTree();
        layerTypeTree.setModel(this.getTree());
        layerTypeTree.setFont(StaticVar.FONT_SourceHanSansCNNormal_12);
        layerTypeTree.setFocusable(false);
        layerTypeTree.addTreeSelectionListener((e) -> {
            var node = (DefaultMutableTreeNode) layerTypeTree.getLastSelectedPathComponent();
            if (node != null && node.isLeaf()) {
                switchLayer(node.getParent().toString(), node.toString());
            }
        });

        layerTypeScrollPane.setViewportView(layerTypeTree);

        this.add(layerTypeScrollPane, BorderLayout.CENTER);
    }

    private DefaultTreeModel getTree() {

        var tree = new DefaultMutableTreeNode("全部图层", true);

        var node_1 = new DefaultMutableTreeNode(OSM_NAME, true);
        node_1.add(new DefaultMutableTreeNode("OpenStreetMap", false));
        tree.add(node_1);

        var node_2 = new DefaultMutableTreeNode(TIAN_NAME, true);
        node_2.add(new DefaultMutableTreeNode("普通图-无标注", false));
        node_2.add(new DefaultMutableTreeNode("地形图-无标注", false));
        node_2.add(new DefaultMutableTreeNode("边界线-无标注", false));
        node_2.add(new DefaultMutableTreeNode("标注层", false));
        tree.add(node_2);

        var node_3 = new DefaultMutableTreeNode(GOOGLE_NAME, true);
        node_3.add(new DefaultMutableTreeNode("普通图-带标注", false));
        node_3.add(new DefaultMutableTreeNode("地形图-带标注", false));
        node_3.add(new DefaultMutableTreeNode("影像图-带标注", false));
        node_3.add(new DefaultMutableTreeNode("影像图-无标注", false));
        node_3.add(new DefaultMutableTreeNode("路网图-带标注", false));
        tree.add(node_3);

        var node_4 = new DefaultMutableTreeNode(AMAP_NAME, true);
        node_4.add(new DefaultMutableTreeNode("普通图-带标注", false));
        node_4.add(new DefaultMutableTreeNode("普通图-无标注", false));
        node_4.add(new DefaultMutableTreeNode("影像图-无标注", false));
        node_4.add(new DefaultMutableTreeNode("路网图-带标注", false));
        node_4.add(new DefaultMutableTreeNode("路网图-无标注", false));
        tree.add(node_4);

        var node_5 = new DefaultMutableTreeNode(TENCENT_NAME, true);
        node_5.add(new DefaultMutableTreeNode("普通图-带标注", false));
        tree.add(node_5);

        var node_6 = new DefaultMutableTreeNode(BING_NAME, true);
        node_6.add(new DefaultMutableTreeNode("普通图1-带标注-全球", false));
        node_6.add(new DefaultMutableTreeNode("普通图1-带标注-国内", false));
        node_6.add(new DefaultMutableTreeNode("普通图1-无标注", false));
        node_6.add(new DefaultMutableTreeNode("普通图2-带标注-全球", false));
        node_6.add(new DefaultMutableTreeNode("普通图2-带标注-国内", false));
        node_6.add(new DefaultMutableTreeNode("普通图2-无标注", false));
        tree.add(node_6);

        var node_7 = new DefaultMutableTreeNode(BING_WGS84_NAME, true);
        node_7.add(new DefaultMutableTreeNode("影像图-无标注", false));
        tree.add(node_7);

        var node_8 = new DefaultMutableTreeNode(BAIDU_NAME, true);
        node_8.add(new DefaultMutableTreeNode("百度瓦片图旧版", false));
        tree.add(node_8);

        var setting = ApplicationSetting.getSetting();
        var addedLayers = setting.getAddedLayers();
        if (addedLayers != null) {
            var node_9 = new DefaultMutableTreeNode(ADDED_NAME, true);
            for (var layer : addedLayers) {
                node_9.add(new DefaultMutableTreeNode(layer.getTitle(), false));
            }
            tree.add(node_9);
        }

        return new DefaultTreeModel(tree);

    }

    private void switchLayer(String parent, String self) {
        switch (parent) {
            case OSM_NAME:
                switch (self) {
                    case "OpenStreetMap" -> browserEngine.sendMessageByWebsocket("SwitchResource", "OpenStreet");
                }
                break;
            case TIAN_NAME:
                switch (self) {
                    case "普通图-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Tianditu-Normal-None");
                    case "地形图-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Tianditu-Terrain-None");
                    case "边界线-无标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Tianditu-Line");
                    case "标注层" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Tianditu-Tip");
                }
                break;
            case GOOGLE_NAME:
                switch (self) {
                    case "普通图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Google-Normal");
                    case "地形图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Google-Terrain");
                    case "影像图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Google-Satellite");
                    case "影像图-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Google-Satellite-None");
                    case "路网图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Google-Street");
                }
                break;
            case AMAP_NAME:
                switch (self) {
                    case "普通图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "AMap-Normal");
                    case "普通图-无标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "AMap-Normal-None");
                    case "影像图-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "AMap-Satellite-None");
                    case "路网图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "AMap-Street");
                    case "路网图-无标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "AMap-Street-None");
                }
                break;
            case TENCENT_NAME:
                switch (self) {
                    case "普通图-带标注" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Tencent-Normal");
                }
                break;
            case BING_NAME:
                switch (self) {
                    case "普通图1-带标注-全球" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-1");
                    case "普通图1-带标注-国内" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-1-CN");
                    case "普通图1-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-1-None");
                    case "普通图2-带标注-全球" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-2");
                    case "普通图2-带标注-国内" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-2-CN");
                    case "普通图2-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Normal-2-None");
                }
                break;
            case BING_WGS84_NAME:
                switch (self) {
                    case "影像图-无标注" ->
                            browserEngine.sendMessageByWebsocket("SwitchResource", "Bing-Satellite-None");
                }
                break;
            case BAIDU_NAME:
                switch (self) {
                    case "百度瓦片图旧版" -> browserEngine.sendMessageByWebsocket("SwitchResource", "Baidu-Normal");
                }
                break;
        }
    }

}
